<template>
    <div class="box">
        <div>
            <el-form :inline="true" :model="searchForm" class="demo-form-inline">
                <el-form-item label="关键字">
                    <el-input v-model="searchForm.keyWord" placeholder="关键字" clearable />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSearch">查询</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div>
            <el-table :data="tableData" stripe style="width: 100%">
                <el-table-column prop="logId" label="日志ID" width="180" />
                <el-table-column prop="logOpt" label="操作者" width="180" />
                <el-table-column prop="logTime" label="操作时间" width="180" />
                <el-table-column prop="logIp" label="操作者IP" width="180" />
                <el-table-column prop="logContent" label="日志内容" width="180">
                    <template #default="scope">
                        <span v-html="scope.row.logContent"></span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" />
            </el-table>
            <div style="margin-top: 20px;">
                <el-pagination
                    v-model:current-page="searchForm.pageNum"
                    v-model:page-size="searchForm.pageSize"
                    :page-sizes="[2, 4, 6, 8]"
                    :background="true"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                />
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { getSysLogByPage } from '@/api/api'


const tableData = ref([]);
const searchForm = ref({
    pageNum: 1,
    pageSize: 2,
    keyWord: '',
});
const total = ref(0);

// 分页获取日志列表
const loadList = () => {
    getSysLogByPage(searchForm.value).then(res => {
        if(res.data.code == 200) {
            tableData.value = res.data.data.records;
            total.value = res.data.data.total;
        }
    })
}

// 每页多少个
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`)
  searchForm.value.pageSize = val;
  // 刷新里表
  loadList();
}

// 切换到哪一页
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`)
  searchForm.value.pageNum = val;
  // 刷新列表
  loadList();
}

// 搜索函数
const onSearch = () => {
    console.log('搜索');
    loadList();
}

// 页面加载钩子函数
onMounted(() => {
    loadList();
});
</script>

<style scoped>

</style>